
var element;
$(document).ready(function(){
    getBasicSetting(0,1);
});
function getBasicSetting(formIndex,id){
    //displayLoading(true);
    $.post("/manage/system/getBasicSetting",{bsid:id},
        function(data){
            if(data.status==1){
                for(var key in data.result){
                    $("form:eq("+formIndex+") [name="+key+"]").val(data.result[key]);
                }
            }
            else if(data.status==4){
                window.location.href="/manage/nopermission.html";
            }
            else if(data.status==5){
                parent.layer.msg(data.msg, {icon: 1});
                window.location.href="/manage/login.html";
            }
            else{
                parent.layer.msg(data.msg, {icon: 1});
            }
            hiddenLoading(true);
        });
}
layui.use('element', function(){
    element = layui.element;
    element.on('tab', function(data){
        //赋值配置ID
        $("form:eq("+data.index+") #bsId").val($(this).attr('data-cid'));
        //读取服务器配置
        getBasicSetting(data.index,$(this).attr('data-cid'));
    });
});
layui.use(['form'], function(){
    var form = layui.form;
    //监听提交
    form.on('submit(demo1)', function(data){
        //JSON.stringify(data.field)
        updateToServer();
        return false;
    });
});
function updateToServer(){
    displayLoading(true);
    $.post("/manage/system/updateBasicSetting",$('.layui-show form').serialize(),
        function(data){
            hiddenLoading(true);
            if(data.status==1){
                parent.layer.msg("更新成功", {icon: 1});
            }
            else if(data.status==4){
                window.location.href="/manage/nopermission.html";
            }
            else if(data.status==5){
                parent.layer.msg(data.msg, {icon: 1});
                window.location.href="/manage/login.html";
            }
            else{
                parent.layer.msg(data.msg, {icon: 1});
            }
        });
}
layui.use('upload', function(){
    var upload = layui.upload;
    //上传图片
    var uploadPictrue = upload.render({
        accept:'file'
        ,size:10240
        ,exts:'mp3'
        ,elem: '.uploadPictrue' //绑定元素
        ,url: '/manage/uploadFile' //上传接
        ,before: function(obj){
            console.info($(this));
            layer.load(); //上传loading
        }
        ,done: function(res){
            if(res.status==1){
                var formIndex = $(".layui-this").attr("data-cid")-1;
                $("form:eq("+formIndex+") :text[name='bsMusic']").val(res.result);
                layer.msg('上传成功', {icon: 6});
            }
            else if(res.status==4){
                window.location.href="/manage/nopermission.html";
            }
            else if(res.status==5){
                layer.confirm(res.msg, {icon: 3, title:'提示'}, function(index){
                    window.location.href="/manage/login.html";
                    layer.close(index);
                });
            }
            else{
                layer.msg(res.msg, {shift: 6});
            }
            layer.closeAll('loading');
        }
        ,error: function(){
            //请求异常回调
            layer.msg('接口异常', {shift: 6});
            layer.closeAll('loading');
        }
    });
});
$(".browseServer").on("click",function(){
    var formIndex = $(".layui-this").attr("data-cid");
    layer.open({
        title:'文件浏览器',
        area: ['80%', '80%'],
        type: 2,
        content: ['/manage/plugins/wxFile/browser.html?formName=bsMusic'+formIndex, 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content:
    });
});